package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IAdminRoleService;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IAdminRoleService adminRoleService;

    @ApiOperation(value = "搜索所有用户")
    @GetMapping("/")
    public List<Admin> getAll(@RequestParam("keywords")String keywords){
        System.out.println(keywords);
        return adminService.selectByName(keywords);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable("id") int id){
        adminRoleService.removeRole(id);
        return RespBean.success("删除成功");
    }

    @ApiOperation(value = "更新用户是否启用")
    @PutMapping("/")
    public RespBean updateEnable(@RequestBody Admin admin){
        adminService.updateEnable(admin);
        return RespBean.success("修改成功");
    }

    @ApiOperation(value = "查询所有角色")
    @GetMapping("/roles")
    public List<Role> getRole(){
        return roleService.list();
    }

    @ApiOperation(value = "修改用户的角色")
    @PutMapping("/role")
    public RespBean updateRole(@RequestParam("adminId")int adminId,@RequestParam("rids")Integer[] ids){
        adminRoleService.updateByAid(adminId,ids);
        return RespBean.success("修改成功");
    }

}
